/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PainelCadastroNotas.java
 *
 * Created on 23/06/2011, 17:28:52
 */
package telas;

import java.sql.SQLException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import projetofinal.*;
import pacoteDao.*;

/**
 *
 * @author Iana
 */
public class PainelCadastroNotas extends javax.swing.JPanel {

    /** Creates new form PainelCadastroNotas */
    public PainelCadastroNotas() {
        initComponents();
        carregaAlunos();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabelNome = new javax.swing.JLabel();
        jLabelDisciplina = new javax.swing.JLabel();
        jLabelNota = new javax.swing.JLabel();
        jTextFieldNota = new javax.swing.JTextField();
        jButtonCadastrar = new javax.swing.JButton();
        jComboBoxAluno = new javax.swing.JComboBox();
        jComboBoxDisciplina = new javax.swing.JComboBox();

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 18));
        jLabel1.setText("CADASTRO DE NOTAS");

        jLabelNome.setText("Aluno:");

        jLabelDisciplina.setText("Disciplina:");

        jLabelNota.setText("Nota:");

        jButtonCadastrar.setText("Cadastrar");
        jButtonCadastrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonCadastrarActionPerformed(evt);
            }
        });

        jComboBoxAluno.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Selecione uma opção:" }));
        jComboBoxAluno.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBoxAlunoActionPerformed(evt);
            }
        });

        jComboBoxDisciplina.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Selecione uma opção:" }));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(99, 99, 99)
                .addComponent(jLabel1)
                .addContainerGap(120, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabelDisciplina)
                    .addComponent(jLabelNome)
                    .addComponent(jLabelNota))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jComboBoxDisciplina, 0, 152, Short.MAX_VALUE)
                    .addComponent(jComboBoxAluno, 0, 152, Short.MAX_VALUE)
                    .addComponent(jTextFieldNota, javax.swing.GroupLayout.DEFAULT_SIZE, 152, Short.MAX_VALUE))
                .addGap(187, 187, 187))
            .addGroup(layout.createSequentialGroup()
                .addGap(157, 157, 157)
                .addComponent(jButtonCadastrar)
                .addContainerGap(162, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(31, 31, 31)
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelNome)
                    .addComponent(jComboBoxAluno, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelDisciplina)
                    .addComponent(jComboBoxDisciplina, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelNota)
                    .addComponent(jTextFieldNota, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(40, 40, 40)
                .addComponent(jButtonCadastrar)
                .addContainerGap(89, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonCadastrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonCadastrarActionPerformed
        // TODO add your handling code here:

        String aluno = (String) jComboBoxAluno.getSelectedItem();
        float nota = Float.parseFloat(jTextFieldNota.getText());
        String disciplina = (String) jComboBoxDisciplina.getSelectedItem();

        Historico historico = new Historico();
        try {
            HistoricoDAO historicoDao = new HistoricoDAO();
            boolean aux = historicoDao.insert(historico);

            if (aux) {
                jComboBoxAluno.removeAllItems();
                jTextFieldNota.setText(null);
                jComboBoxDisciplina.removeAllItems();
            } else {
                this.setVisible(false);
            }
        } catch (SQLException ex) {
            Logger.getLogger(PainelCadastroNotas.class.getName()).log(Level.SEVERE, null, ex);
        }
}//GEN-LAST:event_jButtonCadastrarActionPerformed

    private void jComboBoxAlunoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBoxAlunoActionPerformed
        // TODO add your handling code here:
        jComboBoxDisciplina.removeAllItems();
        carregaAlunos();
        carregaDisciplinas();
    }//GEN-LAST:event_jComboBoxAlunoActionPerformed

    private void carregaAlunos() {

        try {
            AlunoDAO alunoDao = new AlunoDAO();
            List<Aluno> alunos = alunoDao.consultar();
            for (Aluno aluno : alunos) {
                jComboBoxAluno.addItem(aluno.getNome());
            }
        } catch (SQLException ex) {
            Logger.getLogger(PainelCadastroDisciplina.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    private void carregaDisciplinas() {
        try {
            DisciplinaDAO disciplinaDao = new DisciplinaDAO();
            List<Disciplina> disciplinas = disciplinaDao.consultar();
            for (Disciplina disciplina : disciplinas) {
                jComboBoxAluno.addItem(disciplina.getNome());
            }
        } catch (SQLException ex) {
            Logger.getLogger(PainelCadastroDisciplina.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonCadastrar;
    private javax.swing.JComboBox jComboBoxAluno;
    private javax.swing.JComboBox jComboBoxDisciplina;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabelDisciplina;
    private javax.swing.JLabel jLabelNome;
    private javax.swing.JLabel jLabelNota;
    private javax.swing.JTextField jTextFieldNota;
    // End of variables declaration//GEN-END:variables
}
